
// Zepto
var $ = require('./components/zepto/zepto');
require('./components/zepto/event');
require('./components/zepto/ajax');
require('./components/zepto/form');
require('./components/zepto/ie');
require('./components/zepto/touch');

module.exports = $;

// 引入Swiper

var Swiper = require('./components/swiper/swiper.min.js');
var swiperAnimate = require('./components/swiper/swiper.animate1.0.2.min.js');
var swiper = new Swiper ('.swiper-container', {
  pagination: '.swiper-pagination',
  paginationClickable: true,
  direction: 'vertical',
  onInit: function(swiper){ //Swiper2.x的初始化是onFirstInit
    swiperAnimate.swiperAnimateCache(swiper); //隐藏动画元素 
    swiperAnimate.swiperAnimate(swiper); //初始化完成开始动画
  }, 
  onSlideChangeEnd: function(swiper){ 
    swiperAnimate.swiperAnimate(swiper); //每个slide切换结束时也运行当前slide动画
  } 
})  

var wx=require('./components/weixin/jweixin.js');

// ref https://github.com/WICG/EventListenerOptions/pull/30
function isPassive() {
    var supportsPassiveOption = false;
    try {
        addEventListener("test", null, Object.defineProperty({}, 'passive', {
            get: function () {
                supportsPassiveOption = true;
            }
        }));
    } catch(e) {}
    return supportsPassiveOption;
}

function scan(){
    wx.chooseImage({
        count:1,
        sizeType:['original','compressed'],
        sourceType:['album','camera'],
        success:function(res){
            var localIds=res.localIds;
            $('#imgTest').attr('src',localIds[0]);
        }
    })
}
function bcode(){
    wx.scanQRCode({
        needResult: 0, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
        scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
        success: function (res) {
        var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
    }
    });
}




//点击进入
var IScroll = require('./components/iscroll/iscroll');
 $("#mainContainer").hide();
 $(".swiper-container").hide();
// jquery/zepto
$("#enter").tap(function(){

    myScroll = new IScroll('#wrapper', {
        scrollbars: true,
        mouseWheel: true,
        interactiveScrollbars: true,
        shrinkScrollbars: 'scale',
        fadeScrollbars: true
      });
		
		document.addEventListener('touchmove', function (e) { e.preventDefault(); }, isPassive() ? {
        capture: false,
        passive: false
      } : false);

    $(".swiper-container").hide();
    $("#mainContainer").show();
    load();
    $.ajax({
        url:'http://www.seean.online/weixinphp/getsign.php',
        type:"post",
        data:{
            url:window.location.href
        },
        dataType:'json',
        async:true,
        success:function(res){
            wx.config({
				debug:true,
				appId:res.appId,
				timestamp: res.timestamp,
    			nonceStr: res.nonceStr,
    			signature: res.signature,
    			jsApiList:[
    				'chooseImage',
                    'scanQRCode'
    			]
			});

            


            wx.ready(function(){
                $('#fixed').tap(function(){
                    scan();
                });

                $('#bcode').tap(function(){
                    bcode();
                });
            })
        }
    })


     var apiUrl = "http://www.seean.online/api/me.php";
     	$.ajax({
     		type:"get",
     		url:apiUrl,
             dataType:'json',
     		async:true,
     		success:function(data){
                // var data = $.parseJSON(data);
                //console.log(data);
                var html = "";

                html='<li class="me-head">'+
                              '<div class="my-img">'+
                                  '<img src="../images/dong.jpg" alt="">'+
                              '</div>'+
                              '<div class="me-head-l">'+
                                  '<h2>圭木</h2>'+
                                  '<p>Never say never!</p>'+ 
                              '</div>'+
                          '</li>';
                for(var i=0;i<data.length;i++){
                  html+='<li class="me-main">'+
                            '<div class="me-ml">'+
                                '<i class="iconfont '+data[i].iconfont+'" style="color:'+data[i].color+';"></i>'+
                            '</div>'+
                            '<div class="me-mr">'+
                                '<h3>'+data[i].name+'</h3>'+
                                '<p>'+data[i].time+'</p>'+
                            '</div>'+
                        '</li>';
                }    
                
         $("#scroller ul").html(html);
        myScroll.refresh();
          },
        error:function(error){
            alert(1)
        }
    })
})

// function galleryImgs(){
// 	// 从相册中选择图片
// 	console.log("从相册中选择多张图片:");
//     plus.gallery.pick( function(e){
//     	for(var i in e.files){
// 	    	console.log(e.files[i]);
//     	}
//     }, function ( e ) {
//     	console.log( "取消选择图片" );
//     },{filter:"image",multiple:true});
// }
// $('#fixed').tap(function(){
//     galleryImgs();
// })

// // 拍照
// function captureImage(){
// 	var cmr = plus.camera.getCamera();
// 	var res = cmr.supportedImageResolutions[0];
// 	var fmt = cmr.supportedImageFormats[0];
// 	console.log("Resolution: "+res+", Format: "+fmt);
// 	cmr.captureImage( function( path ){
// 			alert( "Capture image success: " + path );  
// 		},
// 		function( error ) {
// 			alert( "Capture image failed: " + error.message );
// 		},
// 		{resolution:res,format:fmt}
// 	);
// }
// $('#bcode').tap(function(){
//     captureImage();
// })



$("#footer ul li").tap(function(){
    var apiTarget = $(this).attr('id');
    if(apiTarget=="me"){
        $('#me').addClass('active').siblings('li').removeClass('active');
        $('#header p').html('me'); 
        var apiUrl = 'http://www.seean.online/www/'+"api/" + apiTarget + ".php";
        $.ajax({
            type:'get',
            url:apiUrl,
              dataType:'json',
     		async:true,
            success:function(data){
                var html = "";
                html='<li class="me-head">'+
                              '<div class="my-img">'+
                                  '<img src="../images/dong.jpg" alt="">'+
                              '</div>'+
                              '<div class="me-head-l">'+
                                  '<h2>圭木</h2>'+
                                  '<p>Never say never!</p>'+
                              '</div>'+
                          '</li>'; 
                for(var i=0;i<data.length;i++){
                  html+='<li class="me-main">'+
                            '<div class="me-ml">'+
                                '<i class="iconfont '+data[i].iconfont+'" style="color:'+data[i].color+';"></i>'+
                            '</div>'+
                            '<div class="me-mr">'+
                                '<h3>'+data[i].name+'</h3>'+
                                '<p>'+data[i].time+'</p>'+
                            '</div>'+
                        '</li>';
                }       
                         
            $("#scroller ul").html(html);
            myScroll.refresh();
         },
        error:function(error){
            alert(2)
        }
    })

 }else if(apiTarget=="skill"){
      $('#skill').addClass('active').siblings('li').removeClass('active');
      $('#header p').html('skill');
    var apiUrl = 'http://www.seean.online/www/'+"api/" + apiTarget + ".php";

     $.ajax({
        type:'get',
        url:apiUrl,
         dataType:'json',
     		async:true,
        success:function(data){
            var html = "";
            for(var i=0;i<data.length;i++){
              html+='<li id="ski">'+
                        '<div class="ski-t">'+
                            '<i class="iconfont '+data[i].iconfont+'"></i>'+
                            '<p>'+data[i].category+'</p>'+
                        '</div>'+
                        '<div class="ski-b">'+
                          '<p style="line-height: 14px;">'+data[i].name+'</p>'+
                          '<p>熟练度：'+data[i].time+'</p>'+
                          '<p>'+data[i].xx+'</p>'+
                        '</div>'+
                    '</li>';
            }
            $("#scroller ul").html(html);
            myScroll.refresh();
        },
        error:function(error){
            alert(3)
        }
    })
 }else if(apiTarget=="project"){
      $('#project').addClass('active').siblings('li').removeClass('active');
      $('#header p').html('project');      
        var apiUrl = 'http://www.seean.online/www/'+"api/" + apiTarget + ".php";
        $.ajax({
            type:'get',
            url:apiUrl,
             dataType:'json',
     		async:true,
            success:function(data){
                var html = "";
                for(var i=0;i<data.length;i++){
                html+='<li id="pro">'+
                          '<div class="exp-img">'+
                                '<img src="'+data[i].image+'">'+
                          '</div>'+
                          '<div class="exp-t">'+
                              '<h2>'+data[i].name+'</h2>'+
                          '</div>'+
                          '<div class="exp-b">'+
                            '<p>'+data[i].category+'</p>'+
                            '<p>'+data[i].detail+'</p>'+
                            '<p>'+data[i].url+'</p>'+
                            '<p>'+data[i].tech+'</p>'+
                          '</div>'+
                      '</li>';
                }
                $("#scroller ul").html(html);
                myScroll.refresh();
            },
            error:function(error){
                alert(4)
            }
        })
}else if(apiTarget=="work"){
      $('#work').addClass('active').siblings('li').removeClass('active');
      $('#header p').html('work');      
        var apiUrl = 'http://www.seean.online/www/'+
        "api/" + apiTarget + ".php";
        $.ajax({
            type:'get',
            url:apiUrl,
             dataType:'json',
     		async:true,
            success:function(data){
             
                var html = "";
                for(var i=0;i<+data.length;i++){
                html+='<li id="wok">'+
                          '<div class="exp-l">'+
                              '<img src="'+data[i].image+'">'+
                              '<h2>'+data[i].category+'</h2>'+
                          '</div>'+
                          '<div class="exp-m">'+
                                '<img src="'+data[i].images+'">'+
                          '</div>'+
                          '<div class="exp-r">'+
                            '<p>'+data[i].name+'</p>'+
                            '<p>'+data[i].time+'</p>'+
                            '<p>'+data[i].posts+'</p>'+
                            '<p>'+data[i].projects+'</p>'+
                          '</div>'+
                      '</li>';
                    }
                    
                    $("#scroller ul").html(html);
                    myScroll.refresh();
            },
            error:function(error){
                alert(5)
            }
        })
   }  

})




var interval  = setInterval(function(){
    if(document.readyState === 'complete'){
        clearInterval(interval);
        $("#loading").hide();
        $(".swiper-container").show();
        swiper.updateContainerSize(); // 万分重要
        swiper.updateSlidesSize(); // 万分重要
    }else{
        $("#loading").show();
    }
})
var myScroll;
function load(){         
    myScroll = new IScroll('#wrapper', {
        scrollbars: true,
        mouseWheel: true,
        interactiveScrollbars: true,
        shrinkScrollbars: 'scale',
        fadeScrollbars: true    

    })
}



